var contextPath = $("#contextPath").val();
var start=0,page=1,showNumber=4,isload=true,isTab=true,state='';
var cashback = $("#cashback");
$(function() {
	dataInit();

	//滚动条
	dropload.scroll(function(t){
		if(t<5&&isload&&isTab){
			$(".lodingStyle").show();
			isload = false;
			dataInit();
		}

	});
	
});

function dataInit(){
	//已返现  未返现的参数
	var	dates = {start:start,showNumber:showNumber,"params['state']":state}
	
		$.ajax({
			url:contextPath+"/user/myvip/mycashback",
			type:"post",
			async:true,
			data:dates,
			dataType:"json",
			success : function(data, textStatus) {
				$(".lodingStyle").hide();
				isTab=true;
				var state = data.result.state;
				if(state == 1){
					var classs;  //颜色
					var tempState;  // 已  /未  返现



					if((data.cashbackMap).length <= 0 ){
						if(data.mystate == '00' || data.mystate == '01'){
							layer.open({
							    title: '提示',
							    content: '您好！您目前尚未有返现记录。',
							    btn: ['我知道了'],
							    yes: function(index){
							    	location.href=contextPath+"/user/myvip/cashback";
							    }
							});	
						}else{
							layer.open({
							    title: '提示',
							    content: '您好！您目前尚未有返现记录。',
							    btn: ['我知道了'],
							    yes: function(index){
							    	location.href=contextPath+"/user/myvip";
							    }
							});	
						}
						
					}else{
						$.each(data.cashbackMap,function(i,v){
							
							var orderID = v.order_id; //订单id  
							var code = v.order_code;
							var order_codes = code.substring(0,4)+orderID+code.substring(code.length-7,code.length); // 生成订单号
							
							var d = new Date()  // 当前系统的时间
							var dt1 = new Date(Date.parse(v.end_time)); // 结束的时间
							var dt2= new Date(Date.parse(v.start_time));//开始的时间
							var dt3 = new Date(Date.parse(v.cashback_time)); // 返现的时间
//							var diff = parseInt((dt1.getTime() - dt2.getTime()) / (1000 * 60 * 60 * 24));  // 共计返现的天数
//							var now = parseInt((dt1.getTime() - d.getTime()) / (1000 * 60 * 60 * 24)); //剩余返现时间(天)
//							var ss = parseInt((d.getTime() - dt2.getTime()) / (1000 * 60 * 60 * 24));   // 开始到现在
							//计划结束时间
							var end = dt1;
							var endYear = dt1.getFullYear()
							var endMon = dt1.getMonth() + 1
							var endDay = dt1.getDate()
							var endtime = endYear+"-"+endMon+"-"+endDay;
						
						//进度条
						var i=0;         
						var mss = dt1.getTime() - dt2.getTime();
						var mms = d.getTime()- dt2.getTime() ;
					
						if (v.state == "01") {  //已返现 ‘01’
							i = 100;
						}else{
							i=((mms/1000 * 60* 60 )/(mss/1000 * 60* 60 ))*100;
						}
						var width = i+"%";
						
						//	已返现  未返现的  时间设置	
						var starttimes = dt3;
						var vYear = starttimes.getFullYear()
						var vMon = starttimes.getMonth() + 1
						var vDay = starttimes.getDate()
						var h = starttimes.getHours(); 
						var m = starttimes.getMinutes(); 
						var se = starttimes.getSeconds();
						var starttime = (vYear+"-"+vMon+"-"+vDay); //未返现的 返現時間
						if (v.state == "01") {
							classs="order_item rebate_completed";
							tempState="已  返现";
							starttime = (vYear+"-"+vMon+"-"+vDay+" "+h+":"+m+":"+se); //已返现的返現時間
						}else{
							classs="order_item";
							tempState="未  返现";
						}
							var type = v.type;
							if(type == 01){
								type = "商品返现";
								cashback.append('<li class="'+classs+'"><div class="order_item_top id="fu"><div class="order_code f0"><a href="'+contextPath+'/order/detail.do?orderID='+order_codes+'"><span class="icon_order_code dib vm"></span><span class="order_code_txt dib vm">订单号 ('+type+')：'+order_codes+' <em class="n">></em></span></a></div><div class="order_status">'+tempState+'</div></div> <div class="order_item_remark rebate_box"><p class="fix c_bright"><span class="l">起始日期</span><span class="r">返现日期</span></p><p class="progressbar"><span class="schedule" style="width:'+width+'"></span></p><p class="fix"><span class="l">'+v.start_time+' </span><span class="r">'+endtime+' </span></p></div><div class="order_item_remark"><div class="fix"><span class="l">返现时间  :'+starttime+'</span><span class="r">返现金额：<em class="n c_bright">&yen; '+v.money+'</em></span></div></div></li>');
								
							}else{
								type = "充值VIP返现";
								cashback.append('<li class="'+classs+'"><div class="order_item_top id="fu"><div class="order_code f0"><span class="icon_order_code dib vm"></span><span class="order_code_txt dib vm">订单号 ('+type+')：'+order_codes+' </span></a></div><div class="order_status">'+tempState+'</div></div> <div class="order_item_remark rebate_box"><p class="fix c_bright"><span class="l">起始日期</span><span class="r">返现日期</span></p><p class="progressbar"><span class="schedule" style="width:'+width+'"></span></p><p class="fix"><span class="l">'+v.start_time+' </span><span class="r">'+endtime+' </span></p></div><div class="order_item_remark"><div class="fix"><span class="l">返现时间  :'+starttime+'</span><span class="r">返现金额：<em class="n c_bright">&yen; '+v.money+'</em></span></div></div></li>');
								
							}
						
						});
						
						start=page*showNumber;
						page++;
						if(data.cashbackMap.length == showNumber){  //滚动条
							isload = true;	
						}
					}
					
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
				isTab=true;
				$(".lodingStyle").hide();
				layer.open({
				    content:textStatus,
				    time: 2
				});
			}
		});		
}

//数据的清空——避免重复加载    以及数据的分页
function paramInit(){
	isload = true;
	$(".lodingStyle").show();
	start=0,page=1;
	cashback.empty();
}
//未返现   已返现的 方法调用
var list = new Tab('.list__tab', {
	  index: 0,
	  onClick: function onClick(tab) {
		  isTab = false;
		  paramInit();
		  if(tab.index == 0){
			  state="";
			  dataInit();
		  }else if(tab.index == 1){
			  state="00";
			  dataInit();
		  }else if(tab.index == 2){
			  state="01";
			  dataInit();
		  }
	  }
	});